package com.jcl.recruit.core.web.utils;

import com.jcl.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName RequestUtils
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/5 9:58
 * @Version 1.0
 **/
public class RequestUtils {

    private static final Logger logger = LoggerFactory.getLogger(RequestUtils.class);

    private static final String X_Forwarded_For           = "X-Forwarded-For";
    private static final String Proxy_Client_IP           = "Proxy-Client-IP";
    private static final String WL_Proxy_Client_IP        = "WL-Proxy-Client-IP";
    private static final String HTTP_CLIENT_IP            = "HTTP_CLIENT_IP";
    private static final String HTTP_X_FORWARDED_FOR      = "HTTP_X_FORWARDED_FOR";
    private static final String unknown                   = "unknown";

    public static boolean isTimeout(HttpServletRequest request){
        if(null == request.getSession()){
            return true;
        }
        if(request.getSession() != null){
            return null == request.getSession().getAttribute(RequestConstants.ATTR_USER_ID_KEY);
        }

        return false;
    }
    /* *
     * @Author yyliu
     * @Description //TODO 
     * @Date 13:58 2018/9/5
     * @Param [request]
     * @return java.lang.String
     **/
    public static String getClientIpAddress(HttpServletRequest request){
        String ip = StringUtils.EMPTY;
        if(request != null){
            ip = request.getHeader(X_Forwarded_For);
            if(null == ip || ip.length() == 0 || unknown.equalsIgnoreCase(ip)){
                ip = request.getHeader(Proxy_Client_IP);
            }
            if(null == ip || ip.length() == 0 || unknown.equalsIgnoreCase(ip)){
                ip = request.getHeader(WL_Proxy_Client_IP);
            }
            if(null == ip || ip.length() == 0 || unknown.equalsIgnoreCase(ip)){
                ip = request.getHeader(HTTP_CLIENT_IP);
            }
            if(null == ip || ip.length() == 0 || unknown.equalsIgnoreCase(ip)){
                ip = request.getHeader(HTTP_X_FORWARDED_FOR);
            }
            if(null == ip || ip.length() == 0 || unknown.equalsIgnoreCase(ip)){
                ip = request.getRemoteAddr();
            }
        }
        return ip;
    }

    /**
     * @Author yyliu
     * @Description
     * @Date 10:14 2018/9/5
     * @Param []
     * @return java.lang.String
     **/
    public static String getUserId(){
        HttpServletRequest request = getHttpServletRequest();
        if(null != request || request.getSession() != null){
            return (String) request.getSession().getAttribute(RequestConstants.ATTR_USER_ID_KEY);
        }
        return null;
    }

    /* *
     * @Author yyliu
     * @Description
     * @Date 10:15 2018/9/5
     * @Param [userId]
     * @return void
     **/
    public static void setUserId(String userId){
        HttpServletRequest request = getHttpServletRequest();
        if(null == request || request.getSession() == null){
            return;
        }
        request.getSession().setAttribute(RequestConstants.ATTR_USER_ID_KEY, userId);
    }

    public static String getCorpCode(){
        HttpServletRequest request = getHttpServletRequest();
        if(null != request || request.getSession() != null){
            return (String) request.getSession().getAttribute(RequestConstants.ATTR_CORP_CODE_KEY);
        }
        return null;
    }

    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 17:23 2018/9/11
     * @Param [corpCode]
     * @return void
     **/
    public static void setCorpCode(String corpCode){
        HttpServletRequest request = getHttpServletRequest();
        if(null == request || request.getSession() == null){
            return;
        }
        request.getSession().setAttribute(RequestConstants.ATTR_CORP_CODE_KEY, corpCode);
    }

    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 10:25 2018/9/5
     * @Param []
     * @return java.lang.String
     **/
    public static String getSessionId(){
        HttpSession session = getSession();
        if(null != session){
            return session.getId();
        }
        return null;
    }

    public static HttpSession getSession(){

        HttpServletRequest request = getHttpServletRequest();
        if(null == request){
            return null;
        }
        return request.getSession();
    }

    public static HttpServletRequest getHttpServletRequest(){

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return request;
    }

    public static HttpServletResponse getHttpServletResponse(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return response;
    }

}
